package com.Product;
import com.enclose.dataBasePool;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.model.info06.*;
@WebServlet("/Product/check_car_shopping")
public class check_car_shopping extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        Integer userId = 0;
        if(req.getParameter("userId") != null){
            userId = Integer.valueOf(req.getParameter("userId"));
        }
        //访问库
        List<info06_son> info06_sonList = new ArrayList<>();
        try {
            dataBasePool dataBasePool = new dataBasePool();
            Statement statement = dataBasePool.getStatement();
            String sql ="SELECT s.shopping_car_id,s.product_id,s.num,p.product_name,p.product_picture,product_selling_price\n" +
                    "FROM shopping_car s\n" +
                    "JOIN product p\n" +
                    "ON s.product_id = p.product_id\n" +
                    "WHERE s.user_id ="+userId;
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                info06_sonList.add(new info06_son(
                        resultSet.getInt("shopping_car_id"),
                        resultSet.getInt("product_id"),
                        resultSet.getInt("num"),
                        resultSet.getString("product_name"),
                        resultSet.getString("product_picture"),
                        resultSet.getString("product_selling_price")
                        ));
            }
            dataBasePool.close(statement,resultSet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        info06_f info06_f = new info06_f();
        info06_f.setCode(200);
        info06_f.setMsg("success!");
        info06_f.setData(info06_sonList);
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(info06_f);
        resp.getWriter().write(str);

    }
}
